home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Blitting Class Library / Buffer Accessors / BufferAccessor.cp next >
Encoding:
Text File  |  1995-11-18  |  2.7 KB  |  116 lines  |  [TEXT/CWIE]

  1. // BufferAccessor.cp, the BufferAccessor abstract base class, to be used for direct
  2. //    access to GWorld pixel-maps, window pixel-maps, and video pixel-maps.
  3.  
  4. // copyright © 1995, Macneil Shonle. All rights reserved.
  5.  
  6. #ifndef __BUFFERACCESSOR__
  7. #include <BufferAccessor.h>
  8. #endif
  9.  
  10. #ifndef __MMUMODESWAPPER__
  11. #include <MMUModeSwapper.h>
  12. #endif
  13.  
  14. // initializes the QuickRowArray pointer
  15. QuickRowArray::QuickRowArray()
  16.     : array(0)
  17. {
  18. }
  19.  
  20. // cleans up allocated memory
  21. QuickRowArray::~QuickRowArray()
  22. {
  23.     delete array;
  24. }
  25.  
  26. // given (1) the base address, (2) the row-bytes and (3) the height, a quick-row array will
  27. // be made to be used for fast row address access without multiplication
  28. //    may throw: xalloc
  29. void QuickRowArray::setArray(PixelPtr base, RowBytes rowBytes, PixelCord height)
  30.     throw(xalloc)
  31. {
  32.     delete array;
  33.     array = new PixelPtr[height];
  34.     if (array == 0) throw xalloc("new failed", "QuickRowArray::setArray");
  35.     
  36.     for (PixelCord y=0; y<height; y++)
  37.         array[y] = base + (y * rowBytes);
  38. }
  39.  
  40. // intentionally left blank
  41. BufferAccessor::BufferAccessor()
  42. {
  43. }
  44.  
  45. // intentionally left blank
  46. BufferAccessor::~BufferAccessor()
  47. {
  48. }
  49.  
  50. // makes a copy of the given BufferAccessor
  51. //    may throw: xalloc
  52. BufferAccessor& BufferAccessor::operator=(const BufferAccessor& buff) throw(xalloc)
  53. {
  54.     theHeight = buff.theHeight;
  55.     theWidth = buff.theWidth;
  56.     theRowBytes = buff.theRowBytes;
  57.     setArray(buff.baseAddr(), buff.rowBytes(), buff.height());
  58.     mmuMode = buff.mmuMode;
  59.     return *this;
  60. }
  61.  
  62. // returns the base-address of the buffer
  63. PixelPtr BufferAccessor::baseAddr() const
  64. {
  65.     return rowAddrs.array[0];
  66. }
  67.  
  68. // returns the row-bytes of the buffer
  69. RowBytes BufferAccessor::rowBytes() const
  70. {
  71.     return theRowBytes;
  72. }
  73.  
  74. // returns the height of the buffer
  75. PixelCord BufferAccessor::height() const
  76. {
  77.     return theHeight;
  78. }
  79.  
  80. // returns the width of the buffer
  81. PixelCord BufferAccessor::width() const
  82. {
  83.     return theWidth;
  84. }
  85.  
  86. // returns the address of the pixel at the given co-ordinates
  87. PixelPtr BufferAccessor::pixelAddr(PixelCord h, PixelCord v) const
  88. {
  89.     return rowAddrs.array[v] + h;
  90. }
  91.  
  92. // returns the address of the row at the given co-ordinate
  93. PixelPtr BufferAccessor::rowAddr(PixelCord r) const
  94. {
  95.     return rowAddrs.array[r];
  96. }
  97.  
  98. // whether or not 32-bit addressing should be used to access the buffer's memory
  99. int BufferAccessor::use32Bit() const
  100. {
  101.     return mmuMode;
  102. }
  103.  
  104. // sets the pixel at co-ordinate (h, v) to the color specified in the last parameter
  105. void BufferAccessor::setPixel(PixelCord h, PixelCord v, Pixel color)
  106. {
  107.     MMUModeSwapper swapTo(use32Bit());
  108.     *pixelAddr(h, v) = color;
  109. }
  110.  
  111. // protected member-function, for derived classes to change the quick-row array
  112. //    may throw: xalloc
  113. void BufferAccessor::setArray(PixelPtr base, RowBytes rb, PixelCord high) throw(xalloc)
  114. {
  115.     rowAddrs.setArray(base, rb, high);
  116. }